package com.app.common;

import com.app.jfinal.interceptor.AuthInterceptor;
import com.app.jfinal.interceptor.CountInterceptor;
import com.app.jfinal.plugin.update.UpdateDbPlugin;
import com.app.web.IndexController;
import com.app.web.adimin.controller.MenuController;
import com.app.web.adimin.controller.RoleController;
import com.app.web.adimin.controller.TemplateController;
import com.app.web.adimin.controller.UserController;
import com.app.web.adimin.model.Menu;
import com.app.web.adimin.model.MenuRole;
import com.app.web.adimin.model.Role;
import com.app.web.adimin.model.Template;
import com.app.web.adimin.model.User;
import com.app.web.adimin.model.UserRole;
import com.app.web.front.controller.BlogCategoryController;
import com.app.web.front.controller.BlogController;
import com.app.web.front.controller.CategoryController;
import com.app.web.front.controller.CommentController;
import com.app.web.front.controller.FavoriteController;
import com.app.web.front.controller.MessageController;
import com.app.web.front.controller.TopicController;
import com.app.web.front.model.Blog;
import com.app.web.front.model.BlogCategory;
import com.app.web.front.model.Category;
import com.app.web.front.model.Comment;
import com.app.web.front.model.Favorite;
import com.app.web.front.model.Message;
import com.app.web.front.model.Reply;
import com.app.web.front.model.Topic;
import com.app.web.tools.controller.ToolsController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class AppConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);			
		me.setViewType(ViewType.FREE_MARKER);
		me.setFreeMarkerViewExtension(".html");
		me.setBaseViewPath("/WEB-INF/view/");
	}
	
	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/user", UserController.class);
		me.add("/role", RoleController.class);
		me.add("/menu", MenuController.class);
		
		me.add("/template", TemplateController.class);
		me.add("/topic", TopicController.class);
		me.add("/category", CategoryController.class);
		me.add("/message", MessageController.class);
		me.add("/comment", CommentController.class);
		me.add("/blog", BlogController.class);
		me.add("/favorite", FavoriteController.class);
		me.add("/blog_category", BlogCategoryController.class);
		me.add("/tools", ToolsController.class);
		
	}

	@Override
	public void configHandler(Handlers me) {
		//me.add(new UrlSkipHandler("/", false));
		me.add(new ContextPathHandler("ctx_path"));
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new AuthInterceptor());
		me.add(new CountInterceptor());
		me.add(new TxByRegex(".*(add|del|edit).*"));
	}

	@Override
	public void configPlugin(Plugins me) {			
		
		C3p0Plugin cp = new C3p0Plugin(loadPropertyFile("jdbc.properties"));
		me.add(cp);
		
		UpdateDbPlugin udp = new UpdateDbPlugin(cp); // 数据库更新插件
		me.add(udp);
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
		me.add(arp);
		arp.setDialect(new MysqlDialect());
		
		me.add(new EhCachePlugin()); 	// 配置缓存
		
		arp.addMapping("user", User.class);
		arp.addMapping("role", Role.class);
		arp.addMapping("menu", Menu.class);
		
		arp.addMapping("topic", Topic.class);
		arp.addMapping("reply", Reply.class);
		arp.addMapping("category", Category.class);
		arp.addMapping("comment", Comment.class);
		arp.addMapping("menu_role", MenuRole.class);
		arp.addMapping("user_role", UserRole.class);
		arp.addMapping("template", Template.class);
		arp.addMapping("message", Message.class);
		arp.addMapping("blog", Blog.class);
		arp.addMapping("favorite", Favorite.class);
		arp.addMapping("blog_category", BlogCategory.class);
		arp.setShowSql(true);
		
	}
	
	public static void main(String[] args) {
		JFinal.start("WebRoot", 82, "/", 5);
	}
}
